//index.js
//获取应用实例
const { request, router, message } = global;
import { ERR_OK } from '../../common/config.js'
const app = getApp()

Page({
  data: {
    isShow: false,
    banner: '',
    logo: '',
    qrcode: '',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    list: [],
    productList: [],
    productId: ''
  },
  onLoad: function (options) {
    let enterprise_id = options.scene || 1
    if (enterprise_id) {
      global.c_id = enterprise_id;
      wx.setStorageSync('PET_COM_ID', enterprise_id);
      this.getCompanyInfoById(enterprise_id)
    }
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse){
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }
  },
  getCompanyInfoById(id) {
    request.post('/api/core/001-getCompanyInfoById', {
      id,
      pageNo: 1,
      limit: 10
    }).then((res) => {
      const data = res.data
      if (data.code === ERR_OK) {
        const info = data.rows[0]
        this.setData({
          banner: info.companyInfo.banner,
          logo: info.companyInfo.logo,
          qrcode: info.companyInfo.qrCode,
          list: info.miniList,
          productList: info.productList
        })
        this.getNetImg('logo', info.companyInfo.logo)
        this.getNetImg('qrCode', info.companyInfo.qrCode)
        global.name = info.companyInfo.name
      }
    }).catch((err) => {
      console.log(err)
    })
  },
  getNetImg(name, url) {
    wx.downloadFile({
      url: url,
      success(res) {
        if (res.statusCode === 200) {
          wx.getImageInfo({
            src: res.tempFilePath,
            success(result) {
              global[name] = {
                path: result.path,
                width: result.width,
                height: result.height
              }
            }
          })
        }
      }
    })
  },
  handleToMiniProgram(e) {
    let index = e.currentTarget.dataset.index
    let list = this.data.list
    let type = list[index].type
    if (type == 1) {
      wx.navigateToMiniProgram({
        appId: list[index].appId,
        success(res) {
          // 打开成功
        }
      })
    }
    if (type == 2) {
      wx.setStorageSync('HTML_CONTENT', list[index].content);
      // console.log(global.content)
      router.navigate({
        url: '/pages/content/content'
      })
    }
  },
  handleGetPic: function(e) {
    let id = e.currentTarget.dataset.id
    this.setData({
      isShow: true,
      productId: id
    })
  },
  cancelGetPic: function() {
    this.setData({
      isShow: false
    })
  },
  handlePhotograph() {
    const self = this
    wx.chooseImage({
      count: 1, //默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['camera'],
      success: function (res) {
        const tempFilePaths = res.tempFilePaths;
        self.cutImg(tempFilePaths[0])
      }
    });
  },
  handleGetToPhoto() {
    const self = this
    wx.chooseImage({
      count: 1, //默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album'],
      success: function (res) {
        const tempFilePaths = res.tempFilePaths;
        self.cutImg(tempFilePaths[0])
      }
    });
  },
  cutImg(path) {
    let id = this.data.productId
    router.navigate({
      url: '/pages/cut-in-side/cut-in-side',
      data: {
        path,
        id
      }
    })
    this.setData({
      isShow: false
    })
  },
  getUserInfo: function(e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  }
})
